package cqrtplm.controller;


import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hustcad.plm.pdm.common.model.dto.search.SearchColumnDTO;
import com.hustcad.plm.pdm.common.model.dto.search.SearchConditionDTO;
import com.hustcad.plm.pdm.common.service.search.TyplmClassifySearchService;
import com.ty.basic.response.ResponseResult;
import com.ty.basic.utils.PageSizeUtil;
import cqrtplm.constant.RouteConstant;
import cqrtplm.dto.RTCertFiledFormDTO;
import cqrtplm.dto.RTCertOAEPANewFormDTO;
import cqrtplm.dto.RTOARETDTO;
import cqrtplm.entity.RTProductDevelopmentOrderDO;
import cqrtplm.entity.RtCertFiledDO;
import cqrtplm.service.RtCertFiledService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import cqrtplm.service.BOMCertificationService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigInteger;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * 机型认证控制器
 */
@RestController
@Api(tags = "认证-机型认证")
@RequestMapping("/rest"+RouteConstant.RATO + "cert/filed/" + "v1")
public class RtCertFiledController {

    @Resource
    private RtCertFiledService rtCertFiledService;
    @Resource
    private BOMCertificationService bomCertificationService;
    @Resource
    private TyplmClassifySearchService typlmClassifySearchService;
    /**
     * 查询机型认证列表
     */
    @PostMapping("/list")
    @ApiOperation(value = "list", notes = "获取机型认证列表")
    public ResponseResult list(@RequestBody RtCertFiledDO rtCertFiled) {
        List<RtCertFiledDO> list = rtCertFiledService.selectRtCertFiledList(rtCertFiled);
        return ResponseResult.success(list);
    }


    /**
     * 分页查询机型认证列表
     */
    @PostMapping("/pagelist")
    @ApiOperation(value = "pagelist", notes = "分页获取机型认证列表")
    public ResponseResult<PageInfo<RtCertFiledDO>> pagelist(
            @RequestBody RtCertFiledDO rtCertFiled,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "100") int pageSize) {
        PageHelper.startPage(page, pageSize);
        List<RtCertFiledDO> list = rtCertFiledService.selectRtCertFiledList(rtCertFiled);
        PageInfo<RtCertFiledDO> pageInfo = new PageInfo<>(list);
        return ResponseResult.success(pageInfo);
    }

    /**
     * 导出机型认证列表
     */
    @PostMapping("/export")
    @ApiOperation(value = "export", notes = "导出机型认证列表")
    public void export(HttpServletResponse response, RtCertFiledDO rtCertFiled) {
        List<RtCertFiledDO> list = rtCertFiledService.selectRtCertFiledList(rtCertFiled);
        // ExcelUtil.listToExcel(list, RtCertFiledDO.class, wwb, "机型认证数据");
    }

    /**
     * 获取机型认证详细信息
     */
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "getInfo", notes = "获取机型认证详情")
    public ResponseResult getInfo(@PathVariable("id") String id) {
        return ResponseResult.success(rtCertFiledService.selectRtCertFiledById(id));
    }

    @GetMapping(value = "/getall")
    @ApiOperation(value = "getAll", notes = "获取机型认证详情")
    public ResponseResult getAllInfo(@RequestParam("id") String id) {
        return ResponseResult.success(rtCertFiledService.selectRtCertFiledAllDataById(id));
    }

    /**
     * 根据型号获取机型认证
     */
    @GetMapping("/model")
    @ApiOperation(value = "getByModel", notes = "根据备案型号和认证获取机型认证")
    public ResponseResult getByModel(@RequestParam("model") String model,@RequestParam("ctype") String ctype) {
        return ResponseResult.success(rtCertFiledService.selectRtCertFiledByModelCtype(model,ctype));
    }

    /**
     * 新增机型认证
     */
    @PostMapping("/add")
    @ApiOperation(value = "add", notes = "新增机型认证")
    public ResponseResult add(@RequestBody RtCertFiledDO rtCertFiled) {
        return ResponseResult.success(rtCertFiledService.insertRtCertFiled(rtCertFiled));
    }

    /**
     * 修改机型认证
     */
    @PostMapping("/edit")
    @ApiOperation(value = "edit", notes = "修改机型认证")
    public ResponseResult edit(@RequestBody RtCertFiledDO rtCertFiled) {
        return ResponseResult.success(rtCertFiledService.updateRtCertFiled(rtCertFiled));
    }

    @PostMapping("/save")
    @ApiOperation(value = "save", notes = "修改机型认证")
    public ResponseResult edit(@RequestBody RTCertFiledFormDTO postdata) {
        return ResponseResult.success(rtCertFiledService.save(postdata));
    }


    @PostMapping("/oasave")
    @ApiOperation(value = "save", notes = "修改机型认证")
    public RTOARETDTO oasave(@RequestBody RTCertOAEPANewFormDTO postdata) {
//        RTOARETDTO ret = new RTOARETDTO();
//        try {
//            rtCertFiledService.oasave(postdata);
//            ret.setSuccess(true);
//            ret.setMessage("保存成功");
//            return ret;
//        } catch (Exception e) {
//            e.printStackTrace();
//            ret.setSuccess(false);
//            ret.setMessage("保存到PLM时出错了:"+e.getMessage());
//        }
        return rtCertFiledService.oasave(postdata);

    }

    /**
     * 删除机型认证
     */
    @DeleteMapping("/del")
    @ApiOperation(value = "delete", notes = "删除机型认证")
    public ResponseResult remove(@RequestBody List<String> ids) {
        return ResponseResult.success(rtCertFiledService.deleteRtCertFiledByIds(ids));
    }

    @PostMapping(value = "/getdevcode")
    @ApiOperation(value = "getdevcode", notes = "获取开发令")
    public ResponseResult getDevCode(@RequestBody RTProductDevelopmentOrderDO  entity) {
        return ResponseResult.success(rtCertFiledService.getDevCode(entity));
//        SearchConditionDTO conditionDTO= new SearchConditionDTO();
//        conditionDTO.setClassificationOid(   entity.getOid() == null ? BigInteger.valueOf(752000397175373824L): entity.getOid());
//        conditionDTO.setKeyword(entity.getOrderNumber());
//        conditionDTO.setNeedThumbnailFile( true);
//        conditionDTO.setPageNum(1);
//        conditionDTO.setPageSize(50);
//        conditionDTO.setTypeInthid("ty.inteplm.part.CTyPart|ProductSpectrum");
//        conditionDTO.setConditionJoint("and");
//        String columnsJson = "[{\"propertyName\":\"thumbnailFile\",\"displayName\":\"缩略图\",\"masterProperty\":false,\"fixed\":\"\",\"width\":\"30\",\"sortable\":false,\"fieldList\":[],\"virtual\":false,\"foreignColumnMaster\":false,\"canNotRemove\":false,\"enum\":false},{\"propertyName\":\"typeIcon\",\"displayName\":\"图形标识\",\"masterProperty\":false,\"fixed\":\"\",\"width\":\"30\",\"sortable\":false,\"fieldList\":[],\"virtual\":false,\"foreignColumnMaster\":false,\"canNotRemove\":false,\"enum\":false},{\"propertyName\":\"identifier\",\"displayName\":\"标识\",\"masterProperty\":false,\"fixed\":\"\",\"width\":\"300\",\"sortable\":false,\"fieldList\":[],\"virtual\":false,\"foreignColumnMaster\":false,\"canNotRemove\":false,\"enum\":false},{\"propertyName\":\"lifecyclestagekeyName\",\"displayName\":\"状态\",\"masterProperty\":false,\"fixed\":\"\",\"width\":\"80\",\"sortable\":false,\"fieldList\":[],\"virtual\":false,\"foreignColumnMaster\":false,\"canNotRemove\":false,\"enum\":false},{\"propertyName\":\"typeDefName\",\"displayName\":\"类型\",\"masterProperty\":false,\"fixed\":\"\",\"width\":\"80\",\"sortable\":false,\"fieldList\":[],\"virtual\":false,\"foreignColumnMaster\":false,\"canNotRemove\":false,\"enum\":false},{\"propertyName\":\"description\",\"displayName\":\"描述\",\"masterProperty\":false,\"fixed\":\"\",\"width\":\"120\",\"sortable\":false,\"fieldList\":[],\"virtual\":false,\"foreignColumnMaster\":false,\"canNotRemove\":false,\"enum\":false},{\"propertyName\":\"ownerDisplayName\",\"displayName\":\"所有者\",\"masterProperty\":false,\"fixed\":\"\",\"width\":\"80\",\"sortable\":false,\"fieldList\":[],\"virtual\":false,\"foreignColumnMaster\":false,\"canNotRemove\":false,\"enum\":false},{\"propertyName\":\"modifystamp\",\"displayName\":\"最后修改时间\",\"masterProperty\":false,\"fixed\":\"\",\"width\":\"160\",\"sortable\":false,\"fieldList\":[],\"virtual\":false,\"foreignColumnMaster\":false,\"canNotRemove\":false,\"enum\":false}]"; // JSON 字符串
//        ObjectMapper mapper = new ObjectMapper();
//        List<SearchColumnDTO> columns = null;
//        try {
//            columns = mapper.readValue(columnsJson, new TypeReference<List<SearchColumnDTO>>() {});
//        } catch (JsonProcessingException e) {
//            throw new RuntimeException(e);
//        }
//        conditionDTO.setColumns(columns);
//        return ResponseResult.success(this.typlmClassifySearchService.queryByCondition(conditionDTO));


    }

    @PostMapping(value = "/bom/validate")
    @ApiOperation(value = "bom/validate", notes = "BOM认证检查")
    public ResponseResult validateBOM(@RequestBody JSONObject req) {
        try {
            // 调用校验方法，传入必要的两个参数
            bomCertificationService.validateBOMCertification(req);
            return ResponseResult.success("BOM物料认证校验通过");
        } catch (Exception e) {
            return ResponseResult.fail("200",e.getMessage());
        }
    }
}